#include "ros/ros.h"

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"

#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>

#include "std_msgs/String.h" 
#include <sstream>
#include <iostream>
#include<stdio.h>

using namespace std;
using namespace cv;


int main(int argc, char  *argv[])
{   
    setlocale(LC_ALL,"");
    ros::init(argc,argv,"talkerr");
    ros::NodeHandle nh;
    image_transport::ImageTransport transport(nh);

    Mat Frame;
    VideoCapture cap("/home/caishuyang/video/red.avi");//这里改成自己的路经

    //检查能否打开视频
    if (!cap.isOpened())
	{
		printf("open video failed!\n");
		return 1;
	}

    image_transport::Publisher pub;
    pub=transport.advertise("OutImage",1);    
    cv_bridge::CvImage cvi;

    //设置发布频率和条件
    ros::Rate r(70);
    bool zhen=1;

    while (ros::ok() && zhen)
    {   
        ros::Time time=ros::Time::now();
        zhen=cap.read(Frame);
        if(zhen){
            //创建发布消息
            cvi.header.stamp = time;
            cvi.header.frame_id = "image";
            cvi.encoding = "bgr8";
            cvi.image = Frame;
            sensor_msgs::Image im;
            cvi.toImageMsg(im);
            pub.publish(im);
        }
        r.sleep();
        ros::spinOnce();
    }


    return 0;
}
